#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>

using namespace std;

#include "Log.hpp"

const string dictname = "./dict.txt";
const string sep = ":";

bool Split(const string& s, string* part1, string* part2)
{
    auto pos = s.find(sep);
    if(pos == string::npos) return false;
    *part1 = s.substr(0, pos);
    *part2 = s.substr(pos + 1);
    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if(!in.is_open())
        {
            log(Fatal, "ifstream open error, errno: %d, errstring: %s", errno, strerror(errno));
            exit(1);
        }

        string line;
        while(getline(in, line))
        {
            string part1, part2;
            Split(line, &part1, &part2);
            dict[part1] = part2;
        }

        in.close();
    }

    string Translation(const string& key)
    {
        auto iter = dict.find(key);
        if(iter == dict.end()) return "Unkonw";
        return iter->second;
    }

    ~Init() {}
private:
    unordered_map<string, string> dict;
};

